package com.thtn.greendroidthtneg2;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class BookmarksActivity extends GDActivity {
	private EditText mEditText;
	private ListView mListView;
	private View view;

	private void showBookmarks() {
		Cursor localCursor = managedQuery(DictionaryProvider.CONTENT_URI, null,
				"", null, null);
		if (localCursor.getCount() == 0) {
			Toast.makeText(getBaseContext(), R.string.no_fav, Toast.LENGTH_LONG)
					.show();
			finish();
			return;
		}
		setTitle(R.string.favorite);
		final MyCursorAdapter localMyCursorAdapter = new MyCursorAdapter(this,
				2130903079, localCursor, new String[] { "suggest_text_1" },
				new int[] { 2131165238 });
		localCursor.setNotificationUri(getContentResolver(),
				DictionaryProvider.CONTENT_URI);
		localMyCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
			public Cursor runQuery(CharSequence paramAnonymousCharSequence) {
				String str = null;
				if (paramAnonymousCharSequence != null)
					str = paramAnonymousCharSequence.toString();
				Cursor localCursor = BookmarksActivity.this.managedQuery(
						DictionaryProvider.CONTENT_URI, null,
						" and suggest_text_1 like'"
								+ str.toLowerCase().replaceAll("'", "''")
								+ "%'", null, null);
				localCursor.moveToFirst();
				localCursor.setNotificationUri(
						BookmarksActivity.this.getContentResolver(),
						DictionaryProvider.CONTENT_URI);
				localMyCursorAdapter.alphaIndexer.setCursor(localCursor);
				return localCursor;
			}
		});
		this.mListView.setAdapter(localMyCursorAdapter);
		this.mListView.setTextFilterEnabled(true);
		this.mListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(
							AdapterView<?> paramAnonymousAdapterView,
							View paramAnonymousView, int paramAnonymousInt,
							long paramAnonymousLong) {
						Intent localIntent = new Intent(BookmarksActivity.this
								.getBaseContext(), WordActivity.class);
						localIntent.putExtra("Bookmark", "Fav<sep>"
								+ paramAnonymousLong);
						BookmarksActivity.this.startActivity(localIntent);
					}
				});
		this.mEditText.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable paramAnonymousEditable) {
				localMyCursorAdapter.getFilter().filter(paramAnonymousEditable);
			}

			public void beforeTextChanged(
					CharSequence paramAnonymousCharSequence,
					int paramAnonymousInt1, int paramAnonymousInt2,
					int paramAnonymousInt3) {
			}

			public void onTextChanged(CharSequence paramAnonymousCharSequence,
					int paramAnonymousInt1, int paramAnonymousInt2,
					int paramAnonymousInt3) {
			}
		});
	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setActionBarContentView(R.layout.wordlist_layout);
		getActionBar().setType(ActionBar.Type.Normal);
		addActionBarItem(ActionBarItem.Type.SortBySize);
		this.mListView = ((ListView) findViewById(R.id.list));
		this.mEditText = ((EditText) findViewById(R.id.editText));
		this.view = findViewById(R.id.view);
		showBookmarks();
	}

	public boolean onHandleActionBarItemClick(ActionBarItem paramActionBarItem,
			int paramInt) {
		switch (paramInt) {
		default:
			return super.onHandleActionBarItemClick(paramActionBarItem,
					paramInt);
		case 0:
			if (this.view.getVisibility() == 8)
				this.view.setVisibility(0);
			while (true) {

				this.mEditText.setText("");
				this.view.setVisibility(8);
				return true;
			}
		}

	}

	public boolean onSearchRequested() {
		return true;
	}

	class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
		AlphabetIndexer alphaIndexer;

		public MyCursorAdapter(Context paramInt, int paramCursor,
				Cursor paramArrayOfString, String[] paramArrayOfInt,
				int[] arrayOfInt) {
			super(paramInt, paramCursor, paramArrayOfString, paramArrayOfInt,
					arrayOfInt);
			this.alphaIndexer = new AlphabetIndexer(paramArrayOfString,
					paramArrayOfString.getColumnIndexOrThrow("suggest_text_1"),
					"#ABCDEFGHIJKLMNOPQRSTUVWXYZ");
			this.alphaIndexer.setCursor(paramArrayOfString);
		}

		public void bindView(View paramView, Context paramContext,
				Cursor paramCursor) {
			WordListActivity.ViewHolder localViewHolder = (WordListActivity.ViewHolder) paramView
					.getTag();
			int i = paramCursor.getPosition();
			int j = getSectionForPosition(i);
			if (getPositionForSection(j) == i) {
				localViewHolder.header.setVisibility(0);
				String str = this.alphaIndexer.getSections()[j].toString()
						.trim();
				localViewHolder.header.setText(str);
			}
			while (true) {
				localViewHolder.textView
						.setText(paramCursor.getString(paramCursor
								.getColumnIndex("suggest_text_1")));

				localViewHolder.header.setVisibility(8);
				return;
			}
		}

		public int getPositionForSection(int paramInt) {
			return this.alphaIndexer.getPositionForSection(paramInt);
		}

		public int getSectionForPosition(int paramInt) {
			return this.alphaIndexer.getSectionForPosition(paramInt);
		}

		public Object[] getSections() {
			return this.alphaIndexer.getSections();
		}

		public View newView(Context paramContext, Cursor paramCursor,
				ViewGroup paramViewGroup) {
			View localView = BookmarksActivity.this.getLayoutInflater()
					.inflate(2130903081, paramViewGroup, false);
			WordListActivity.ViewHolder localViewHolder = new WordListActivity.ViewHolder();
			localViewHolder.header = ((TextView) localView
					.findViewById(2131165240));
			localViewHolder.textView = ((TextView) localView
					.findViewById(2131165241));
			localView.setTag(localViewHolder);
			return localView;
		}
	}
}
